package com.ly.service.impl;

import com.ly.dao.CooperationApplicationMapper;
import com.ly.dao.CustomerSupportRequestMapper;
import com.ly.dao.FileMapper;
import com.ly.pojo.domain.CooperationApplicationDO;
import com.ly.pojo.domain.CustomerSupportRequestDO;
import com.ly.pojo.domain.FileDO;
import com.ly.pojo.dto.CooperationApplicationDTO;
import com.ly.pojo.dto.CustomerSupportRequestDTO;
import com.ly.pojo.query.CooperationApplicationQuery;
import com.ly.pojo.query.CustomerSupportRequestQuery;
import com.ly.pojo.query.FileQuery;
import com.ly.pojo.vo.PageResult;
import com.ly.service.CooperationAndSupportService;
import java.lang.Boolean;
import java.lang.Object;
import java.lang.Override;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ly.exception.BusinessException;
import java.util.List;
import java.util.Map;
import java.io.File;
import com.ly.constant.ResultCodeConstant;
import java.io.IOException;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合作与客户支持管理的实现
 *
 * @author Administrator
 * @date 2025-03-07 10:51:13
 */
@Service
public class CooperationAndSupportServiceImpl implements CooperationAndSupportService {

    @Autowired
    private CooperationApplicationMapper cooperationApplicationMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CustomerSupportRequestMapper customerSupportRequestMapper;

    @Autowired
    private FileMapper fileMapper;

    @Override
    public Boolean addCooperationApplication(CooperationApplicationDTO cooperationApplicationDTO) {
        CooperationApplicationDO existingApplication = cooperationApplicationMapper.selectByCompanyNameAndContactPerson(cooperationApplicationDTO.getCompanyName(), cooperationApplicationDTO.getContactPerson());
        if (existingApplication != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        CooperationApplicationDO cooperationApplicationDO = new CooperationApplicationDO();
        cooperationApplicationDO.setCompanyName(cooperationApplicationDTO.getCompanyName());
        cooperationApplicationDO.setContactPerson(cooperationApplicationDTO.getContactPerson());
        cooperationApplicationDO.setContactEmail(cooperationApplicationDTO.getContactEmail());
        cooperationApplicationDO.setContactPhone(cooperationApplicationDTO.getContactPhone());
        cooperationApplicationDO.setApplicationStatus("pending");
        cooperationApplicationDO.setCreateBy("system");
        cooperationApplicationDO.setCreateTime(new Date());
        cooperationApplicationDO.setUpdateBy("system");
        cooperationApplicationDO.setUpdateTime(new Date());
        int result = cooperationApplicationMapper.insert(cooperationApplicationDO);
        return result > 0;
    }

    @Override
    public PageResult<CooperationApplicationDO> getCooperationApplicationList(CooperationApplicationQuery cooperationApplicationQuery) {
        int totalRecords = cooperationApplicationMapper.countApplications();
        // 修改分页逻辑，确保参数类型与 PageResult 构造函数匹配
        long totalPages = totalRecords / (long) cooperationApplicationQuery.getPageSize() + (totalRecords % cooperationApplicationQuery.getPageSize() > 0L ? 1L : 0L);
        List<CooperationApplicationDO> applications = cooperationApplicationMapper.selectApplicationsByPage(cooperationApplicationQuery.getPageIndex(), cooperationApplicationQuery.getPageSize());
        // 调整参数顺序和类型
        return new PageResult<>((long) totalRecords, totalPages, (long) cooperationApplicationQuery.getPageIndex(), applications);
    }

    @Override
    public Boolean updateCooperationApplicationStatus(CooperationApplicationDTO cooperationApplicationDTO) {
        CooperationApplicationDO existingApplication = cooperationApplicationMapper.selectById(cooperationApplicationDTO.getApplicationId());
        if (existingApplication == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        CooperationApplicationDO cooperationApplicationDO = new CooperationApplicationDO();
        cooperationApplicationDO.setId(cooperationApplicationDTO.getApplicationId());
        cooperationApplicationDO.setApplicationStatus(cooperationApplicationDTO.getApplicationStatus());
        cooperationApplicationDO.setUpdateBy("system");
        cooperationApplicationDO.setUpdateTime(new Date());
        int result = cooperationApplicationMapper.updateById(cooperationApplicationDO);
        return result > 0;
    }

    @Override
    public Boolean addCustomerSupportRequest(CustomerSupportRequestDTO customerSupportRequestDTO) {
        CustomerSupportRequestDO customerSupportRequestDO = new CustomerSupportRequestDO();
        customerSupportRequestDO.setCustomerName(customerSupportRequestDTO.getCustomerName());
        customerSupportRequestDO.setCustomerEmail(customerSupportRequestDTO.getCustomerEmail());
        customerSupportRequestDO.setCustomerPhone(customerSupportRequestDTO.getCustomerPhone());
        customerSupportRequestDO.setRequestContent(customerSupportRequestDTO.getRequestContent());
        customerSupportRequestDO.setRequestStatus("pending");
        customerSupportRequestDO.setCreateBy("system");
        customerSupportRequestDO.setCreateTime(new Date());
        customerSupportRequestDO.setUpdateBy("system");
        customerSupportRequestDO.setUpdateTime(new Date());
        int result = customerSupportRequestMapper.insert(customerSupportRequestDO);
        return result > 0;
    }

    @Override
    public PageResult<CustomerSupportRequestDO> getCustomerSupportRequestList(CustomerSupportRequestQuery customerSupportRequestQuery) {
        int totalRecords = customerSupportRequestMapper.countRequests();
        long totalPages = totalRecords / customerSupportRequestQuery.getPageSize() + (totalRecords % customerSupportRequestQuery.getPageSize() > 0L ? 1L : 0L);
        List<CustomerSupportRequestDO> requests = customerSupportRequestMapper.selectRequestsByPage(customerSupportRequestQuery.getPageIndex(), customerSupportRequestQuery.getPageSize());
        // 修改参数顺序以匹配 PageResult 构造函数
        return new PageResult<>((long) totalRecords, totalPages, (long) customerSupportRequestQuery.getPageIndex(), (long) customerSupportRequestQuery.getPageSize(), requests);
    }

    @Override
    public Boolean updateCustomerSupportRequestStatus(CustomerSupportRequestDTO customerSupportRequestDTO) {
        CustomerSupportRequestDO existingRequest = customerSupportRequestMapper.selectById(customerSupportRequestDTO.getRequestId());
        if (existingRequest == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        CustomerSupportRequestDO customerSupportRequestDO = new CustomerSupportRequestDO();
        customerSupportRequestDO.setId(customerSupportRequestDTO.getRequestId());
        customerSupportRequestDO.setRequestStatus(customerSupportRequestDTO.getRequestStatus());
        customerSupportRequestDO.setUpdateBy("system");
        customerSupportRequestDO.setUpdateTime(new Date());
        int result = customerSupportRequestMapper.updateById(customerSupportRequestDO);
        return result > 0;
    }

    @Override
    public Boolean uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        String uploadDir = request.getServletContext().getRealPath("/uploads");
        File uploadPath = new File(uploadDir);
        if (!uploadPath.exists()) {
            uploadPath.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        String filePath = uploadPath + File.separator + fileName;
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        FileDO fileDO = new FileDO();
        fileDO.setFileName(fileName);
        fileDO.setFilePath(filePath);
        fileDO.setFileType(file.getContentType());
        fileDO.setCreateBy("system");
        fileDO.setCreateTime(new Date());
        fileDO.setUpdateBy("system");
        fileDO.setUpdateTime(new Date());
        int result = fileMapper.insert(fileDO);
        return result > 0;
    }

    @Override
    public PageResult<FileDO> getFileList(FileQuery fileQuery) {
        int totalRecords = fileMapper.countFiles();
        long totalPages = totalRecords / fileQuery.getPageSize() + (totalRecords % fileQuery.getPageSize() > 0L ? 1L : 0L);
        List<FileDO> files = fileMapper.selectFilesByPage(fileQuery.getPageIndex(), fileQuery.getPageSize());
        return new PageResult<>((long) totalRecords, totalPages, (long) fileQuery.getPageIndex(), (long) fileQuery.getPageSize(), files);
    }
}
